package com.onlinetask.model.typehandler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.*;
import java.text.DateFormat;

/**
 * Created with IntelliJ IDEA.
 * User: liyuanliu
 * Date: 13-11-14
 * Time: 上午9:42
 * To change this template use File | Settings | File Templates.
 */
public class DateTypeHandler implements TypeHandler{
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType) throws SQLException {
        Date date = null;
        try{
              date = (Date)o;
          }catch (ClassCastException e){
              throw new TypeHandlerException("DateTypeHandler:入参转化为日期类型异常",e);
          }
          preparedStatement.setString(i,date.getTime()+"");
    }

    @Override
    public Object getResult(ResultSet resultSet, String s) throws SQLException {
        String rs = resultSet.getString(s);
        return new Date(Long.parseLong(rs));  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Object getResult(ResultSet resultSet, int i) throws SQLException {
        String rs = resultSet.getString(i);
        return new Date(Long.parseLong(rs));  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Object getResult(CallableStatement callableStatement, int i) throws SQLException {
        String rs = callableStatement.getString(i);
        return new Date(Long.parseLong(rs));  //To change body of implemented methods use File | Settings | File Templates.
    }
}
